package com.lansi.company.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lansi.bean.ResponseResult;
import com.lansi.company.bean.Company;
import com.lansi.company.service.ICompanyService;

@Controller
@RequestMapping("/lansi")
public class CompanyController {
	//依赖注入
	@Resource
	private ICompanyService companyService;
	
	//公司注册
	@RequestMapping("/registerCompany.do")
	@ResponseBody
	public ResponseResult<Void> RegisterCompany(
			/*String name, 
			String sn, 
			String type, 
			Integer gwnumber,
			Integer cardnumber,
			String addr,
			String memo*/
			@RequestBody Map<String,Object> map
			){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
			String name        =(String) map.get("name");
			String sn          =(String) map.get("sn");
			String type 	   =(String) map.get("type");
			Integer gwnumber   =(Integer)map.get("gwnumber");
			Integer cardnumber =(Integer)map.get("cardnumber");
			String addr        =(String) map.get("addr");
			String memo        =(String) map.get("memo");
			
			System.out.println("company注册 "+"name:"+name+" sn:"+sn+" type:"+type+" gwnumber:"+gwnumber+" cardnumber:"+cardnumber+" addr:"+addr+" memo:"+memo);
			Company company = new Company();
			company.setName(name);
			company.setSn(sn);
			company.setType(type);
			company.setGwnumber(gwnumber);
			company.setCardnumber(cardnumber);
			company.setAddr(addr);
			company.setMemo(memo);
			
			companyService.register(company);
			rr.setState(1);
			rr.setMessage("注册成功");
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());		
		}
		return rr;		
	}
	
	
	//处理批量删除公司信息和单个删除公司信息
	@RequestMapping("/delCompany.do")
	@ResponseBody
	public ResponseResult<Void> delCompany(/*Integer[] ids*/ @RequestBody Map<String,Integer[]> map){
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
			Integer[] ids = map.get("ids");
			System.out.println("删除人员个数:"+ids.length);
			if(ids.length>1) {
				companyService.delCompany(ids);
				rr.setState(1);
				rr.setMessage("删除成功");
			}else {
				Integer id = ids[0];
				companyService.delCompanyById(id);
				rr.setState(1);
				rr.setMessage("删除成功");
			}
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		
		return rr;		
	}
	
	//修改公司信息
	@RequestMapping("/updateCompany.do")
	@ResponseBody
	public ResponseResult<Void> updateCompany(
			/*Integer id,
			String name, 
			String sn, 
			String type, 
			Integer gwnumber,
			Integer cardnumber,
			String addr,
			String memo*/
			@RequestBody Map<String,Object> map
			){
		ResponseResult<Void> rr = new  ResponseResult<Void>();
		try {
			Integer id         =(Integer)map.get("id");
			String  name       =(String) map.get("name");
			String  sn         =(String) map.get("sn");
			String  type 	   =(String) map.get("type");
			Integer gwnumber   =(Integer)map.get("gwnumber");
			Integer cardnumber =(Integer)map.get("cardnumber");
			String  addr       =(String) map.get("addr");
			String  memo       =(String) map.get("memo");
			companyService.updateCompany(id, name, sn, type, gwnumber, cardnumber, addr, memo);
			rr.setState(1);
			rr.setMessage("修改成功");
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		return rr;
		
	}
	
	//根据条件查询出所有的公司信息
	@RequestMapping("/getCompany.do")
	@ResponseBody
	public ResponseResult<List<Company>> getCompany(
			/*Integer page,
			String name,
			String sn,
			Integer type*/
			@RequestBody Map<String,Object> map
			){
		ResponseResult<List<Company>> rr = new ResponseResult<List<Company>>();
		try {
			Integer page       =(Integer)map.get("page");
			String  name       =(String) map.get("name");
			String  sn         =(String) map.get("sn");
			Integer type 	   =(Integer) map.get("type");
			
			if(page==null) {
				page=1;
			}
			Integer offset = (page-1)*10;//跳过的页数
			List<Company> list = companyService.getCompany(name, sn, type, offset, 10);
			System.out.println("查询company用户结果:"+list);
			Integer Companycount = companyService.getCompanycount(name, sn, type);
			System.out.println("查询到的数量:"+Companycount);
			rr.setData(list);
			rr.setState(1);
			rr.setMessage(Companycount.toString());
		} catch (RuntimeException e) {
			rr.setMessage(e.getMessage());
			rr.setState(-1);			
		}
		return rr;
		
	}
	
	//得到sn
	@RequestMapping("/getSn.do")
	@ResponseBody
	public ResponseResult<Company> getSn(String sn){
		ResponseResult<Company> rr = new ResponseResult<Company>();
		try {
			Company company = companyService.getSn(sn);
			rr.setState(1);
			rr.setMessage("查询成功");
			rr.setData(company);
			
		} catch (RuntimeException e) {
			rr.setMessage(e.getMessage());
			rr.setState(-1);			
		}
		return rr;
		
	}
}
